@extends('backend.layouts.base')
@section('title', '幻灯片管理')
@section('content')
    <link rel="stylesheet" href="/static/css/backend/detail.css">
    <div class="all">
        <div id="app" v-cloak>
            <div class="vue-crumbs">
                <a @click="goParent()">幻灯片管理</a> > 编辑幻灯片
            </div>
            <el-form ref="form" :model="form" :rules="rules" label-width="15%">
                <div class="vue-main">
                    <div class="vue-main-title">
                        <div class="vue-main-title-left"></div>
                        <div class="vue-main-title-content">编辑幻灯片</div>
                    </div>
                    <div class="vue-main-form">
                        <el-form-item label="排序">
                            <el-input v-model="form.display_order" type="number" ></el-input>
                        </el-form-item>
                        <el-form-item label="幻灯片标题" prop="title">
                            <el-input v-model="form.title"></el-input>
                        </el-form-item>
                        <el-form-item label="图片" prop="thumb">
                            <div class="upload-box" @click="openUpload('thumb')" v-if="!form.thumb">
                                <i class="el-icon-plus" style="font-size:32px"></i>
                            </div>
                            <div @click="openUpload('thumb')" class="upload-boxed" v-if="form.thumb">
                                <img :src="form.thumb" alt="" style="width:150px;height:150px;border-radius: 5px;cursor: pointer;">
                                <div class="upload-boxed-text">点击重新上传</div>
                            </div>
                        </el-form-item>
                        <el-form-item label="幻灯片链接">
                            <el-input v-model="form.link"></el-input>
                        </el-form-item>
                        <el-form-item label="是否显示">
                            <template>
                                <el-switch
                                        v-model="form.status"
                                        active-value="1"
                                        inactive-value="0"
                                >
                                </el-switch>
                            </template>
                        </el-form-item>
                    </div>
                </div>

            </el-form>
            <div class="vue-page">
                <div class="vue-center">
                    <el-button type="primary" @click="submitForm('form')">提交</el-button>
                    <el-button @click="goBack">返回</el-button>
                </div>
            </div>
            <upload-img :upload-show="uploadShow" :name="chooseImgName" @replace="changeProp" @sure="sureImg"></upload-img>
        </div>

    </div>
    @include('backend.public.uploadImg')
    <script>
        var app = new Vue({
            el:"#app",
            delimiters: ['[[', ']]'],
            name: 'test',
            data() {
                return{
                    id:0,
                    form:{
                        display_order:'',
                        title:'',
                        thumb:'',
                        link:'',
                        status:'1',
                        carousel_id:'',
                    },
                    rules:{
                        title:{ required: true, message: '请输入名称'},
                        thumb:{ required: true, message: '请上传图片'}
                    },
                    submit_url:'',
                    uploadShow:false,
                    chooseImgName:'',

                }
            },
            created() {

            },
            mounted() {
                this.id = getParam("id");
                this.form.carousel_id = getParam('carousel_id');
                if(this.id) {
                    this.is_edit = true;
                    this.submit_url = `{!! absolute_admin_url('sys.carouselItem.update') !!}`;
                    this.getData();
                }
                else {
                    this.submit_url = `{!! absolute_admin_url('sys.carouselItem.create') !!}`;
                }
            },
            methods: {
                goParent() {
                    window.location.href = `{!! absolute_admin_url('sys.carouselItem.index') !!}` + '&id='+this.form.carousel_id;
                },
                getData() {
                    let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                    this.$http.post('{!! absolute_admin_url('sys.carouselItem.getDetail') !!}',{id:this.id}).then(function (response) {
                            if (response.data.result){
                                this.form = {
                                    ...response.data.data,
                                };
                            }
                            else {
                                this.$message({message: response.data.msg,type: 'error'});
                            }
                            loading.close();
                        },function (response) {
                            this.$message({message: response.data.msg,type: 'error'});
                            loading.close();
                        }
                    );
                },
                submitForm(formName) {
                    let that = this;
                    let json = {
                        display_order:this.form.display_order,
                        title:this.form.title,
                        thumb:this.form.thumb,
                        link:this.form.link,
                        status:this.form.status,
                        carousel_id:this.form.carousel_id
                    };
                    if(this.id) {
                        json.id = this.id
                    }
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                            this.$http.post(this.submit_url,{params:json}).then(response => {
                                if (response.data.result) {
                                    this.$message({type: 'success',message: '操作成功!'});
                                    this.goParent();
                                } else {
                                    this.$message({message: response.data.msg,type: 'error'});
                                }
                                loading.close();
                            },response => {
                                loading.close();
                            });
                        }
                        else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                },

                goBack() {
                    history.go(-1)
                },
                openUpload(str) {
                    this.chooseImgName = str;
                    this.uploadShow = true;
                },
                changeProp(val) {
                    if(val == true) {
                        this.uploadShow = false;
                    }
                    else {
                        this.uploadShow = true;
                    }
                },
                sureImg(name,image,image_url) {
                    this.form[name] = image_url;
                },
                clearImg(str) {
                    this.form[str] = "";
                    this.$forceUpdate();
                },
            },
        })

    </script>
@endsection
